//
//  RecordCardViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class RecordCardViewController: KMViewController {
    
    fileprivate let cellId = "recordCardCell"
    
    fileprivate var dataArray = [RecordCardModel]()
    
    fileprivate var tableView : UITableView!
    
    init(title:String?) {
        super.init(nibName: nil, bundle: nil)
        
        self.title = title
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        getData()
        createTableView()
    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(RecordCardCell.self, forCellReuseIdentifier: cellId)
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    //MARK: 获取数据
    fileprivate func getData() {
        
        KMTool.requestMethod(.post, URLString: url_applicationRecord, parameters: ["user_name":KMTool.stringForEnumKey(.phone) ?? ""], success: { (value, json) in
            
                if json["status"].stringValue == "0" {
                    if let array = json["data"].arrayObject as? [[String:Any]] {
                        
                        for item in array {
                            let model = RecordCardModel.setModel(with: item)
                            self.dataArray.append(model)
                        }
                        
                        self.tableView.reloadData()
                    }
                    
                }
        }) {
            
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}

extension RecordCardViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 200)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? RecordCardCell
        cell?.model = self.dataArray[indexPath.row]
        cell?.inquireBtn.tag = indexPath.row
        cell?.goonBtn.tag = indexPath.row
        cell?.inquireBtn.addTarget(self, action: #selector(progressQuery(sender:)), for: .touchUpInside)
        cell?.goonBtn.addTarget(self, action: #selector(goonApply(sender:)), for: .touchUpInside)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    //MARK: 进度查询
    @objc fileprivate func progressQuery(sender:UIButton) {
        
        let serviceVC = ServiceViewController.init(urlString: "/mobile/schedule", title: progressInquiry)
        self.navigationController?.pushViewController(serviceVC, animated: true)
    }
    
    //MARK: 继续申请
    @objc fileprivate func goonApply(sender:UIButton) {
        
        var urlString = ""
        
        if let m_link = dataArray[sender.tag].m_link,let link = dataArray[sender.tag].link {

            if m_link != "" {
                urlString = m_link
            }else if link != "" {
                urlString = link
            }
            
            if urlString == "" {
                return
            }
            //路径编码
            urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        }else{
            return
        }
        
        let webDetailsVC = WebDetailsViewController.init(urlString: urlString, title: nil)
        self.navigationController?.pushViewController(webDetailsVC, animated: true)
    }
    
}
